<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 14px 28px rgba(0,0,0,0.10), 0 10px 10px rgba(0,0,0,0.08);
            padding: 40px;
            width: 400px;
            max-width: 90%;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #777;
            font-size: 14px;
        }

        .input-group {
            margin-bottom: 20px;
            position: relative;
        }

        .input-group input {
            width: 100%;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border 0.3s;
        }

        .input-group input:focus {
            border-color: #4a90e2;
            outline: none;
            box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
        }

        .input-group label {
            position: absolute;
            left: 15px;
            top: 15px;
            color: #999;
            font-size: 16px;
            transition: all 0.3s;
            pointer-events: none;
        }

        .input-group input:focus + label,
        .input-group input:not(:placeholder-shown) + label {
            top: -10px;
            left: 10px;
            font-size: 12px;
            background-color: white;
            padding: 0 5px;
            color: #4a90e2;
        }

        button {
            width: 100%;
            padding: 15px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #357ab8;
        }

        .forgot-password {
            text-align: center;
            margin-top: 20px;
        }

        .forgot-password a {
            color: #777;
            text-decoration: none;
            font-size: 14px;
            transition: color 0.3s;
        }

        .forgot-password a:hover {
            color: #4a90e2;
        }

        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 5px;
            text-align: center;
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-right: 10px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <h1>欢迎回来</h1>
        <p>请输入您的用户名和密码登录</p>
    </div>

    <form id="loginForm">
        <div class="input-group">
            <input type="text" id="username" placeholder=" " required>
            <label for="username">用户名</label>
        </div>

        <div class="input-group">
            <input type="password" id="password" placeholder=" " required>
            <label for="password">密码</label>
        </div>

        <div id="errorMessage" class="error-message" style="display: none;"></div>

        <button type="submit" id="loginButton">
            <span id="buttonText">登 录</span>
            <span id="buttonLoading" class="loading" style="display: none;"></span>
        </button>

        <div class="forgot-password">
            <a href="#">忘记密码?</a>
        </div>
    </form>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', async function(e) {
        e.preventDefault();

        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        const errorMessage = document.getElementById('errorMessage');
        const loginButton = document.getElementById('loginButton');
        const buttonText = document.getElementById('buttonText');
        const buttonLoading = document.getElementById('buttonLoading');

        // 显示加载状态
        loginButton.disabled = true;
        buttonText.textContent = '登录中...';
        buttonLoading.style.display = 'inline-block';
        errorMessage.style.display = 'none';

        try {
            // 调用登录API
            const response = await fetch('/v1/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            });

            const data = await response.json();

            if (response.ok) {
                // 登录成功处理
                console.log('登录成功:', data);

                // 这里可以存储token或跳转到其他页面
                // localStorage.setItem('token', data.token);
                // window.location.href = '/dashboard';

                alert('登录成功!');
            } else {
                // 登录失败处理
                console.error('登录失败:', data);
                errorMessage.textContent = data.message || '用户名或密码错误';
                errorMessage.style.display = 'block';
            }
        } catch (error) {
            console.error('请求失败:', error);
            errorMessage.textContent = '网络错误，请稍后重试';
            errorMessage.style.display = 'block';
        } finally {
            // 重置按钮状态
            loginButton.disabled = false;
            buttonText.textContent = '登 录';
            buttonLoading.style.display = 'none';
        }
    });
</script>
</body>
</html>